<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        echo 'hello world !';
        $x = 100;
        $y = 100;
        $z = $x + $y;
        echo "<p>$z</p>";
        printf("<p>答案=$z</p>");

        function test() {
            $a = 10;
            global $x;
            echo "a=$a";
            echo "x=$x";
            echo $GLOBALS['z']; // 全局参数

            echo "<br/>";
            static $b = 0;
            echo $b;
            $b++;
        }

        test();
        test();
        echo '<br/>';
        echo '这是一个', '字符串', '使用了', '多个拼接';
        $array = array("abc", "dfc", "brfsdfa");
        echo '<br/>';
        var_dump($array);

        print "123";
        print "sbf";
        echo '<br/>';

        function print_vars($obj) {
            foreach (get_object_vars($obj) as $p => $val) {
                echo "\t$p=$val\n";
            }
        }

        // 自动加载引用类
        spl_autoload_register(function($className) {
//            echo $className . PHP_EOL;
//            exit;
//            echo str_replace("//", "\\", $className . ".php"),"<br/>";
            require str_replace("//", "\\", $className . ".php");
        });


        $car = new Cars\Car("yellow");

        print_vars($car);

        $result = $car->getMyCar();

        echo '我的车是', $result, '颜色的<br/>';

        $bmw = new Cars\BMW();
        echo "<br/>";
        var_dump($bmw);

        $audi = new Cars\Audi();
        $audi->setColor("red");
        echo "<br>";
        $audi->getColor();
        echo "<br>";
        var_dump($audi);
        echo "<br>";

        $myCar = new Cars\MyCar();

        $myResult = $myCar->getMyCar();

        echo '我的车是', $myResult, '颜色的<br/>';

        $bcar = new Cars\BCar();

        $bcar->setCarParam("red", "suv");

        $bcar->getCarParam();

        echo "<br>";
        // 继承抽象方法
        $acar = new Cars\AExtendCar();

        $acar->prefixValue("hi menla ");

        $acar->printOut();

        // 调用静态方法
        $acar::getMyACar();

        echo "<br>";

        $market = array("Volvo", "BMW", "Toyota");

        echo "I like " . $market[0] . "," . $market[1] . "and " . $market[2] . "<br>";

        var_dump($market);

        // 多维数组
        $arr = array(
            array("a", "b"),
            array("c"),
            array("e", "f")
        );

        var_dump($arr);
        echo '<br/>';
        for ($x = 0; $x < count($arr); $x++) {
//            var_dump($arr[$x]);
//            echo '<br/>';

            for ($j = 0; $j < count($arr[$x]); $j++) {
                var_dump($arr[$x][$j]);
                echo '<br/>';
            }
        }
        echo '<br/>';
        // php关联数组
        $age = array("menla" => "24", "tina" => "20", "peter" => "33");

        echo '未排序前:<br/>';
        var_dump($age);
        echo '<br/>';
        foreach ($age as $x => $x_value) {
            echo 'name=' . $x . ",age=" . $x_value . "<br>";
        }
        echo '<br/>';
//        sort($age);
        asort($age);
        echo 'asort() - 根据关联数组的值，对数组进行升序排列 排序后:<br/>';
        var_dump($age);
        echo '<br/>';

        foreach ($age as $x => $x_value) {
            echo 'name=' . $x . ",age=" . $x_value . "<br>";
        }
        echo '<br/>';

        ksort($age);
        echo 'ksort() - 根据关联数组的键，对数组进行升序排列 排序后:<br/>';
        var_dump($age);
        echo '<br/>';

        foreach ($age as $x => $x_value) {
            echo 'name=' . $x . ",age=" . $x_value . "<br>";
        }
        echo '<br/>';

        $relateArr = array("menla" => array("24", "girl"), "tina" => array("20", "boy"), "peter" => "33");
//        var_dump($relateArr);
        echo '<br/>';

        foreach ($age as $x => $x_value) {
            echo 'name=' . $x . ",age=" . $x_value . "<br>";
        }
        echo '<br/>';


        foreach ($relateArr as $x => $x_value) {//gettype($arry);//array
            if (is_array($x_value)) {// is_array、is_string、is_int、is_double
                for ($i = 0; $i < count($x_value); $i++) {
                    echo 'name=' . $x . ",age=" . $x_value[$i] . "<br>";
                }
            } else {
                echo 'name=' . $x . ",age=" . $x_value . "<br>";
            }
        }
        echo '<br/>';

        ksort($relateArr);

        echo '排序后：<br/>';
        foreach ($relateArr as $x => $x_value) {//gettype($arry);//array
            if (is_array($x_value)) {// is_array、is_string、is_int、is_double
                for ($i = 0; $i < count($x_value); $i++) {
                    echo 'name=' . $x . ",age=" . $x_value[$i] . "<br>";
                }
            } else {
                echo 'name=' . $x . ",age=" . $x_value . "<br>";
            }
        }
        echo '<br/>';

        echo 'sort() - 对数组进行升序排列<br>';
        echo 'rsort() - 对数组进行降序排列<br>';
        echo 'asort() - 根据关联数组的值，对数组进行升序排列<br>';
        echo 'ksort() - 根据关联数组的键，对数组进行升序排列<br>';
        echo 'arsort() - 根据关联数组的值，对数组进行降序排列<br>';
        echo 'krsort() - 根据关联数组的键，对数组进行降序排列<br>';

        echo $_SERVER['PHP_SELF'] . "<br>";
        echo $_SERVER['SERVER_NAME'] . "<br>";
        echo $_SERVER['HTTP_HOST'] . "<br>";
        echo $_SERVER['SERVER_ADDR'] . "<br>";
        echo $_SERVER['HTTP_USER_AGENT'] . "<br>";
        echo "当前执行脚本的绝对路径--" . $_SERVER['SCRIPT_FILENAME'] . "<br>";
        echo "访问的页面--" . $_SERVER['SERVER_ADMIN'] . "<br>";
        $i = 1;
        while ($i < 5) {
            echo 'while 循环 --' . $i . "<br>";
            $i++;
        }

        $i = 1;
        do {
            echo 'do...while 循环 --' . $i . "<br>";
            $i++;
        } while ($i < 5);

        for ($j = 1; $j < 5; $j++) {
            echo 'for 循环 --' . $j . "<br>";
        }

        foreach ($age as $a) {
            echo 'foreach 循环 --' . $a . "<br>";
        }


        $aArray = array(2, 1, 3, 5, 2, 10, 33, 23);
        for ($i = 0; $i < count($aArray); $i++) {
            for ($j = $i + 1; $j < count($aArray); $j++) {
                if ($aArray[$i] > $aArray[$j]) {
                    $tmp = $aArray[$i];
                    $aArray[$i] = $aArray[$j];
                    $aArray[$j] = $tmp;

                    print_r($aArray);
                    echo '<br>';
                }
            }
        }
        echo __LINE__ . '<br>';
        echo __FILE__ . '<br>';
        echo __DIR__ . "<br>";
        echo __TRAIT__ . "<br>";


//        /* 非限定名称 */
//        foo(); // 解析为 Foo\Bar\foo resolves to function Foo\Bar\foo
//        foo::staticmethod(); // 解析为类 Foo\Bar\foo的静态方法staticmethod。resolves to class Foo\Bar\foo, method staticmethod
//        echo FOO; // resolves to constant Foo\Bar\FOO
//
//        /* 限定名称 */
//        subnamespace\foo(); // 解析为函数 Foo\Bar\subnamespace\foo
//        subnamespace\foo::staticmethod(); // 解析为类 Foo\Bar\subnamespace\foo,
//                                          // 以及类的方法 staticmethod
//        echo subnamespace\FOO; // 解析为常量 Foo\Bar\subnamespace\FOO

        /* 完全限定名称 */
//        \Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo
//        \Foo\Bar\foo::staticmethod(); // 解析为类 Foo\Bar\foo, 以及类的方法 staticmethod
//        echo \Foo\Bar\FOO; // 解析为常量 Foo\Bar\FOO

        echo "date 函数<br>";
        date_default_timezone_set("Asia/Shanghai");
        echo date('y-m-d h:m:s') . "<br>";
        echo date('Y-M-d H:i:s') . "<br>";
        echo date('Y-m-d H:i:sA') . "<br>";
        echo "当前时间是 " . date("H:i:sA")."<br>";
//        include 'filename';
//        require 'filename';


        $d1 = strtotime("December 31");
        $d2 = ceil(($d1 - time()) / 60 / 60 / 24);
        echo "距离十二月三十一日还有：" . $d2 . " 天。";
        ?>
    </body>
</html>
